package com.chebaohui.iqs.service.impl.deal.bihu.utils;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

/**
 * 壁虎get请求工具方法
 * 
 * @author xielonghai
 *
 */
public class BHHttpClientUtil {
	/**
	 * GET请求
	 * 
	 * @param httpClient
	 * @param httpContext
	 * @param url
	 *            路径
	 * @return
	 * @throws Exception
	 */
	public static String _httpGetToBody(String url) throws Exception {
		CloseableHttpClient httpClient = null;
		HttpClientContext httpContext = null;
		CloseableHttpResponse response = null;
		try {
			httpClient = createSSLClientDefault();
			httpContext = HttpClientContext.create();
			response = _httpGetToResponse(httpClient, httpContext, url);
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			throw new Exception("GET:"+url
					+"\n"+"<普通>请求异常:" + e.getMessage());
		} finally {
			try {
				if (null != response)
					response.getEntity().getContent().close();
				if (null != httpClient)
					httpClient.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	/**
	 * GET请求
	 */
	private static CloseableHttpResponse _httpGetToResponse(CloseableHttpClient httpClient,
			HttpClientContext httpContext, String url) throws Exception {
		//HttpGet httpGet = new HttpGet(URLEncoder.encode(url,"utf-8"));//
		HttpGet httpGet = new HttpGet(url);
		RequestConfig requestconfing = RequestConfig.custom()
				.setConnectionRequestTimeout(1000*120)
				.setSocketTimeout(1000 * 120)
				.setConnectTimeout(1000 * 120)
				.build();
		httpGet.setConfig(requestconfing);
		CloseableHttpResponse response = httpClient.execute(httpGet, httpContext);
		return response;
	}
	
	/**连接配置
	 * @return
	 */
	private static RequestConfig getConfig(){
		return RequestConfig.custom()
				.setConnectionRequestTimeout(1000*120)
				.setSocketTimeout(1000 * 120)
				.setConnectTimeout(1000 * 120)
				.setStaleConnectionCheckEnabled(true)
				.build();
	}

	/**
	 * https 必须用此方法创建httpclient实例(需要证书文件，适合通用HTTPS)
	 * 
	 * @return CloseableHttpClient实体
	 */
	public static CloseableHttpClient createSSLClientDefault() {
		try {
			SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
				// 信任所有
				@SuppressWarnings("unused")
				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					return true;
				}

				@Override
				public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
						throws java.security.cert.CertificateException {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
			return HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(getConfig()).build();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
		return HttpClients.createDefault();
	}
}
